package com.film.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.film.domain.User;
import com.film.service.UserService;
import com.film.service.impl.UserServiceImpl;
/**
 * 该控制类用来做用户头像的修改
 * @author 2119196781
 *
 */
@WebServlet("/updateHeadImg.do")
public class UpdateHeadImgController extends HttpServlet{
	private UserService  userService = new UserServiceImpl();
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//创建factory工厂
				DiskFileItemFactory factory = new DiskFileItemFactory();
				
				//servletFileUpload对象
				ServletFileUpload servletFileUpload = new ServletFileUpload(factory);
				servletFileUpload.setHeaderEncoding("UTF-8");
				List<String> list = new ArrayList<>();
				String path="";
				String name="";
				try {

					List<FileItem> items = servletFileUpload.parseRequest(request);
		            //遍历
					for(FileItem item:items){
						//首先判断是普通文本项还是文件项，文件项与普通项由不同的API来处理
						if(item.isFormField()){
							//true表示普通文本项
							//获取文本项的name属性值
							//是普通的组件
							String value = item.getString("UTF-8");
						
							list.add(value);
						}else{
							//false表示文件项
							//获取文件名称
							name=item.getName();
							Long time = new Date().getTime();
							
							String realName ="img/user_img/"+time+""+name;
							String filmname = time+""+name;
							list.add(realName);
							//获取文件项的输入流
							InputStream in=item.getInputStream();
							//获取服务器端文件存储的目标磁盘路径
							path=getServletContext().getRealPath("img/user_img");
							System.out.println(path);
							//获取输出流，输出到本地文件中
							File file = new File(path);
							if(!file.exists()) {
								file.mkdir();
								
							}
							if(!"".equals(name)) {
								File files = new File(path+"/"+filmname);
								OutputStream out=new FileOutputStream(path+"/"+filmname);
////								//写入数据
								int len=0;
								byte[] b=new byte[1024];                    
								while((len=in.read(b))!=-1){
									out.write(b,0,len);
								}
								in.close();
								out.close();
							}
						}
					}
					//更换图片之前删除用户之前的图片
					User u= userService.selectAll(Integer.parseInt(list.get(0)));
					String lastImgAddress = u.getuImg().substring(u.getuImg().lastIndexOf("/")+1);
					//进行图片的修改
					System.out.println(list.get(1));
					if(!"".equals(name)) {
						File file = new File(path+"/"+lastImgAddress);
						file.delete();
						int result = userService.updateImg(Integer.parseInt(list.get(0)), list.get(1));
						if( result ==1) {
							request.setAttribute("msg",true);
						}
					}
					response.sendRedirect("userEssential.do?uId="+list.get(0));
					
				} catch (FileUploadException e) {
					e.printStackTrace();
				}
	}
}
